package com.netflexitysolutions.amazonws.ec2.internal.operations;

import java.util.List;
import java.util.Set;

import com.amazonaws.ec2.doc._2009_04_04.TerminateInstancesInfoType;
import com.amazonaws.ec2.doc._2009_04_04.TerminateInstancesItemType;
import com.amazonaws.ec2.doc._2009_04_04.TerminateInstancesResponseType;
import com.amazonaws.ec2.doc._2009_04_04.TerminateInstancesType;
import com.netflexitysolutions.amazonws.ec2.EC2;
import com.netflexitysolutions.amazonws.ec2.InstanceTerminationResult;
import com.netflexitysolutions.amazonws.ec2.internal.ModelUtil;

public class TerminateInstancesOperation extends ObjectSetOperationExecutor<TerminateInstancesType, List<InstanceTerminationResult>> {

	public TerminateInstancesOperation(EC2 ec2, Set<String> objectIds) {
		super(ec2, objectIds);
		if (objectIds == null || objectIds.size() == 0) {
			throw new IllegalArgumentException("At least one instance id should be specified");
		}
	}

	@Override
	protected List<InstanceTerminationResult> call(TerminateInstancesType request) {
		TerminateInstancesInfoType set = new TerminateInstancesInfoType();
		List<TerminateInstancesItemType> list = set.getItem();
		for (String instanceId : objectIds) {
			TerminateInstancesItemType item = new TerminateInstancesItemType();
			item.setInstanceId(instanceId);
			list.add(item);
		}
		request.setInstancesSet(set);
		TerminateInstancesResponseType response = getEC2().getService().terminateInstances(request);
		return ModelUtil.toDomainType(response.getInstancesSet());
	}

}
